<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\CateController;
use DB;
class HomeController extends Controller
{
	/**
	 * 无限极分类在前台展示 
	 */
    public function index(Request $request){

         // 获取无限极分类数据
        $data = CateController::getCatePid(0); 

         // 获取推荐商品数据
        $goodsrecom = DB::table('tbl_goodsrecom')
                    ->join('tbl_goods','tbl_goods.id','=','tbl_goodsrecom.gid')
                    ->get();

        // 商品活动数据获取
        $goodsaction = DB::table('tbl_goodsaction')
                  ->join('tbl_goods','tbl_goods.id','=','tbl_goodsaction.actiongid')
                  ->get();
        //商品显示分类模块
        //先关联查询所有的商品数量
        $cate = DB::table('tbl_goods')
             ->join('tbl_types','tbl_goods.goodstype','=','tbl_types.typename')
             ->get();
        //将分类进行遍历 以便取其ID
        foreach($data as $k=>$v){
            // dd($v);   $v['id']是顶级分类的ID
            foreach($cate as $kk=>$vv){
                //判断如果顶级分类的ID在商品表中的path中那么便属于此类别的商品
                //并且在前台让其显示8条
                if(in_array($v['id'],explode(',',$vv['path']))&&($kk<8)){
                    //将满足条件的商品塞入一个自定义数组arr中（实为三维数组）
                    $arr[$v['typename']][] = $vv;//$vv是商品遍历  $v是顶级分类遍历结果
                }
            }
        }
               //友情链接数据获取
        $friendlink = DB::table('tbl_friendlink')->get(); 
        $webconfg = DB::table('tbl_webconfg')->get(); 
        session(['webname'=>$webconfg[0]['webname'],'keywords'=>$webconfg[0]['keywords'],'description'=>$webconfg[0]['description'],'logo'=>$webconfg[0]['logo'],'copyright'=>$webconfg[0]['copyright']]);
        // // 将数据传给首页   
        return view('home.home.index',['data'=>$data,'recom'=>$goodsrecom,'request'=>$request->all(),'goodsaction'=>$goodsaction,'arr'=>$arr,'friendlink'=>$friendlink]); 
    }
}
